// pages/danger/checkUp/checkUp.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inspectPublicList: [],
        page: 1,
        reachBottom: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadInspectPublic()
    },

    /**
     * 加载当前用户创建的排查检查表
     */
    loadInspectPublic() {
        var that = this
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };
        var params = {
            createUserId: app.globalData.user.userId,
            type: 2,//2是隐患排查
            status: 15 //状态是已经结束的
        }
        wx.request({
            header: header,
            url: app.globalData.baseUrl + '/safe/danger/inspectPublic/find',
            method: "POST",
            data: params,
            success(res) {
                console.log(res)
                var inspectPublicList = res.data.data.list
                for (var i = 0; i < inspectPublicList.length; i++) {
                    inspectPublicList[i].createTimeStr = util.formatTime(new Date(inspectPublicList[i].createTime))
                    inspectPublicList[i].statusName = util.getInspectStatusName(inspectPublicList[i].status)

                }
                that.setData({
                    inspectPublicList: inspectPublicList
                })
            }
        })
    },
    /**
     * 进入发布的隐患自查
     */
    inspectPublic: function (e) {
        var inspectPublicId = e.currentTarget.dataset.inspectpublicid
        wx.navigateTo({
            url: '/pages/danger/myHistoryInspect/index?inspectId=' + inspectPublicId + '&inspect=false',
        })
    },

    /**
     * 扫码检查
     * @param e
     */
    scanQrCode: function (e) {
        if (e.currentTarget.dataset.type == 1) {
            wx.navigateTo({
                url: '/pages/danger/companySelectors/index',
            })
        } else {
            wx.scanCode({
                success: (res) => {
                    console.log(res)
                    var qrCodeJson = JSON.parse(res.result)
                    this.setData({
                        inspectId: res.result
                    })
                    var companyId = 0;
                    if (e.currentTarget.dataset.type == 0 || e.currentTarget.dataset.type == 2) {
                        companyId = app.globalData.user.companyId
                    } else {
                        companyId = qrCodeJson.companyId
                    }
                    //TODO
                    // wx.navigateTo({
                    //     url: '/pages/danger/selfAdd/index?inspectId=' + qrCodeJson.inspectId + '&inspect=true' +
                    //     '&type=' + e.currentTarget.dataset.type + '&deptId=' + qrCodeJson.deptId +
                    //     '&deptName=' + qrCodeJson.deptId + '&companyId=' + companyId +
                    //     '&companyName=' + qrCodeJson.companyName,
                    // })
                    var inspectId=qrCodeJson.inspectId
                    wx.navigateTo({
                        url: '/pages/danger/self/selfStart/selfStart?inspectType='+2
                        +"&inspectId="+inspectId+"&companyName="+app.globalData.user.companyName
                        +"&companyId="+companyId+"&deptId="+app.globalData.user.deptId
                        +"&deptName="+app.globalData.user.deptName,
                    })
                }
            })
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        // 显示加载图标
        wx.showLoading({
            title: '玩命加载中',
        })
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };
        var params = {
            createUserId: app.globalData.user.userId,
            type: 2,
            pageCurrent: this.data.page + 1,
            status: 15
        }
        wx.request({
            header: header,
            url: app.globalData.baseUrl + '/safe/danger/inspectPublic/find',
            method: "POST",
            data: params,
            success(res) {
                console.log(res)
                wx.hideLoading()
                if (that.data.reachBottom) {
                    wx.showToast({
                        title: '没有更多数据',
                    })
                } else {
                    var newSelfInspectPublicList = res.data.data.list
                    var oldSelfInspectPublicList = that.data.inspectPublicList
                    for (var i = 0; i < newSelfInspectPublicList.length; i++) {
                        newSelfInspectPublicList[i].createTimeStr = util.formatTime(new Date(newSelfInspectPublicList[i].createTime))
                        newSelfInspectPublicList[i].statusName = util.getInspectStatusName(newSelfInspectPublicList[i].status)
                        oldSelfInspectPublicList.push(newSelfInspectPublicList[i])
                    }
                    that.setData({
                        inspectPublicList: oldSelfInspectPublicList,
                        page: that.data.page + 1
                    })
                    if (res.data.data.isLastPage) {
                        that.setData({
                            reachBottom: true
                        })
                    }
                }
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})